<?php

/**
 * @Author: liu
 * @Date:   2017-10-31 14:34:42
 * @Last Modified by:   liu
 * @Last Modified time: 2018-01-26 12:27:23
 */

class PriceBargaining extends Eloquent
{
    protected $table = "price_bargaining";

  
   /**
    * @Author   lyt                      <ahlyt@anhuibj.com>
    * @DateTime 2017-10-31T16:09:48+0800
    * @param    int supply_id 标准耗材id
    * @param    int shop_id   耗材id
    * @return   int 0 不能编辑 1 能编辑价格
    */
    public static function ischange($supply_id,$shop_id){
    	$si=SupplyItem::getCacheItemInfo($supply_id);

        //优先判断耗材设置没设置价格额如果没设置 则能编辑
        $ss_temp=SupplySub::where("supply_id",$si->id)->where("status",1)->first();
        if(is_null($ss_temp)||$ss_temp->usual_price==0||$si->type==1){
            return 1;
        }
    	$st=Report::get_part_classification($si->st_classifi_id);        
        $pb=PriceBargaining::leftJoin("price_bargaining_shop","price_bargaining_shop.price_id","=","price_bargaining.id")->where("price_bargaining.status",1)
        ->where("price_bargaining_shop.status",1)->where("price_bargaining_shop.shop_id",$shop_id)->select("price_bargaining.*")->get();												//获取该门店适用的所有的
        
        $falg=0;																					//默认都不能编辑价格 如果符合一条修改价格规则 则能修改价格
        foreach ($pb as $_pb) {
        	if($_pb->s_sc1==0&&$_pb->supply_brand_id==0){											//如果该条规则 都为空 忽略该条规则
        		continue;
        	}
        	$temp_supply=false;
        	if($_pb->supply_id!=0){																	//说明该规则指定耗材
        		if($si->id==$_pb->supply_id)$temp_supply=true;
        	}else if($_pb->supply_series_id!=0){
        		if($si->series_id==$_pb->supply_series_id) $temp_supply=true;
        	}else if($_pb->supply_brand_id!=0){
        		if($si->brand_id==$_pb->supply_brand_id) $temp_supply=true;
        	}else{
        		$temp_supply=true;
        	}
        	$temp_fenlei=false;
        	if($_pb->s_sc3!=0){																		//说明该规则指定三级分类
        		if($st['level_3_id']==$_pb->s_sc3) $temp_fenlei=true;
        	}else if($_pb->s_sc2!=0){
        		if($st['level_2_id']==$_pb->s_sc2) $temp_fenlei=true;
        	}else if($_pb->s_sc1!=0){
        		if($st['level_1_id']==$_pb->s_sc1) $temp_fenlei=true;
        	}else{
        		$temp_fenlei=true;
        	}
        	if($temp_fenlei&&$temp_supply){															//说明两个个都符合条件
        		return 1;
        	}
        }      
        return $falg;
    }
}